﻿
#include "Test/LoadResources.h"


ALoadResources::ALoadResources()
{
	PrimaryActorTick.bCanEverTick = true;

	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
	RootComponent = StaticMeshComponent;

	AssetPath = TEXT("Texture2D'/Game/Assets/Textures/R-C.R-C'");
	StaticLoadResource();
}

void ALoadResources::BeginPlay()
{
	Super::BeginPlay();
	
}

void ALoadResources::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// 静态加载资源。
void ALoadResources::StaticLoadResource()
{
	ConstructorHelpers::FObjectFinder<UTexture2D> Texture(*AssetPath);
	if (Texture.Succeeded())
	{
		UTexture2D* Texture2d = Texture.Object;
	}
}

// 动态加载资源，由蓝图调用。
bool ALoadResources::DynaLoadResource(const FString& Path)
{
	const UTexture2D* Texture2D = LoadObject<UTexture2D>(nullptr, *AssetPath);
	if (!Texture2D)
	{
		GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, TEXT("Texture2D 加载失败"));
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, TEXT("Texture2D 加载成功"));
	}
	
	UStaticMesh* Mesh = LoadObject<UStaticMesh>(nullptr, *Path);
	if (Mesh)
	{
		StaticMeshComponent->SetStaticMesh(Mesh);
		return true;
	}
	return false;
}

bool ALoadResources::ModifyMaterialColor(const FString& Path)
{
	UMaterialInterface* IMat = LoadObject<UMaterialInterface>(nullptr, *Path);
	if (!IMat)
	{
		return false;
	}
	UMaterialInstanceDynamic* Mat = UMaterialInstanceDynamic::Create(IMat, nullptr);
	StaticMeshComponent->SetMaterial(0, Mat);

	// UMaterialInterface* Mat = StaticMeshComponent->GetMaterial(0);

	FColor Color = FColor(255,0,0);
	Mat->SetVectorParameterValue(FName(TEXT("BaseColor")), FLinearColor(Color));

	
	return true;
}




